package com.tchintech.pay.view.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.AllMoneyAdapter;
import com.tchintech.pay.delegate.AllMoneyDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.Money;
import com.tchintech.pay.model.entity.PayDetail;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.PullToRefreshView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class AllMoneyActivity extends ActivityPresenter<AllMoneyDelegate> implements View.OnClickListener, PullToRefreshView.OnFooterRefreshListener {
    private ListView listView;
    private AllMoneyAdapter allMoneyAdapter;
    private List<Money> list;
    private Handler handler;
    private String money_url = "Money.MoneyList";
    private Map<String, String> map;
    private PullToRefreshView pullToRefreshView;
    /**
     * 记录查询的多少个
     */
    private int start_location = 0;
    //1:全记录   2：只提现记录
    private int state = 0;

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTitles("账户记录");
        pullToRefreshView = viewDelegate.getPull();
        viewDelegate.setOnClickListener(this, R.id.iv_back);
        listView = viewDelegate.getListView();
        pullToRefreshView.setOnFooterRefreshListener(this);
        allMoneyAdapter = new AllMoneyAdapter(this, list);
        listView.setAdapter(allMoneyAdapter);
        handler();
        query();
    }

    @Override
    protected void initData() {
        super.initData();
        list = new ArrayList<Money>();
        state = getIntent().getIntExtra("state", 1);

    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.MONEY_LIST:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                JSONArray jsonArray = json.getJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    setList(jsonArray);
                                } else {
                                    showMsg("没有可加载的记录了");
                                }
                            } else {
                                showMsg("查询失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };
    }

    private void setList(JSONArray jsonarray) {
        try {
            start_location = start_location + jsonarray.length();
            for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject jsonObject = jsonarray.getJSONObject(i);
                Money money = new Money();
                money.setMoney(jsonObject.getString("statement"));
                money.setWay(jsonObject.getString("des"));
                list.add(money);
                allMoneyAdapter.setList(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 当前用户的金额
     */
    private void query() {
        map = new HashMap<String, String>();
        map.put("user_id", AppConst.getUser().getId());
        map.put("start_location", start_location + "");
        map.put("state", state + "");
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, money_url, map, HttpCode.MONEY_LIST);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    protected Class<AllMoneyDelegate> getDelegateClass() {
        return AllMoneyDelegate.class;
    }

    @Override
    public void onFooterRefresh(final PullToRefreshView view) {
        view.postDelayed(new Runnable() {
            public void run() {
                //上拉事件
                //query(start_location);
                view.onFooterRefreshComplete();
            }
        }, 1000);
    }
}
